package com.situ.player.activity;

import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.situ.player.R;
import com.situ.player.service.MusicService;

import java.util.ArrayList;

import static android.R.attr.path;
import static com.situ.player.R.id.seekBar;

public class MusicPlayActivity extends AppCompatActivity implements View.OnClickListener{
    // lgot
    private static final String TAG = "MusicPlayActivity";
    private static SeekBar mSeekBar;
    private static TextView mTvProgress;
    private static TextView mTvTotal;
    private ImageView mIvPrevious;
    private ImageView mIvPlay;
    private ImageView mIvNext;

    // 正在播放的歌曲的路径
    private String mPath;
    // 当前目录下面所有的歌曲
    private ArrayList<String> mMp3List;
    // 记录当前正在播放歌曲在ArrayList中位置
    private Integer mCurrentMusicIndex;
    private MusicService.MusicControll mMusicController;

    public static Handler handler = new Handler(){
        public void handleMessage(android.os.Message msg) {
            Bundle bundle = msg.getData();
            // 总时长
            int duration = bundle.getInt("duration");
            // 当前播放时长
            int currentPosition = bundle.getInt("currentPosition");
            mSeekBar.setMax(duration);
            mSeekBar.setProgress(currentPosition);

            // 显示总时长
            int minute = duration / 1000 / 60;
            int second = duration / 1000 % 60;
            mTvTotal.setText(minute + ":" + second);

            // 显示播放时长
            int currentMinute = currentPosition / 1000 / 60;
            int currentSecond = currentPosition / 1000 % 60;
            mTvProgress.setText(currentMinute + ":" + currentSecond);
        };
    };

    private ServiceConnection mConnection = new ServiceConnection() {
        // 当Service绑定上之后会回调这个方法
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.d(TAG, "onServiceConnected: ");
            mMusicController = (MusicService.MusicControll) service;
            mMusicController.init(mPath);
        }

        // 当Service断开时候会回调这个方法
        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.d(TAG, "onServiceDisconnected: ");
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_music_play);
        initView();
        initData();
        bindMyService();
    }

    private void bindMyService() {
        Intent intent = new Intent(this, MusicService.class);
        bindService(intent, mConnection, BIND_AUTO_CREATE);
    }

    private void initData() {
        Intent intent = getIntent();
        mPath = intent.getStringExtra("path");
        // logd
        Log.d(TAG, "onCreate: path: " + mPath);
        mMp3List = (ArrayList<String>) intent.getSerializableExtra("mp3List");
        mCurrentMusicIndex = mMp3List.indexOf(mPath);
    }

    private void initView() {
        mSeekBar = (SeekBar) findViewById(seekBar);
        mTvProgress = (TextView) findViewById(R.id.tv_progress);
        mTvTotal = (TextView) findViewById(R.id.tv_total);
        mIvPrevious = (ImageView) findViewById(R.id.iv_previous);
        mIvPlay = (ImageView) findViewById(R.id.iv_play);
        mIvNext = (ImageView) findViewById(R.id.iv_next);
        mIvPrevious.setOnClickListener(this);
        mIvPlay.setOnClickListener(this);
        mIvNext.setOnClickListener(this);

        mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            // 停止滑动时候处理
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // 得到当前拖动停止的位置
                int progress = seekBar.getProgress();
                // 调用服务的seekTo方法改变音乐的进度
                mMusicController.seekTo(progress);
            }

            // 开始滑动时候处理
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            // 滑动条变化时候吹
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                                          boolean fromUser) {
                // TODO Auto-generated method stub

            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_previous:
                if (mCurrentMusicIndex == 0) {
                    Toast.makeText(this, "当前已经是第一首了", Toast.LENGTH_SHORT).show();
                    return;
                }
                mCurrentMusicIndex--;
                mMusicController.init(mMp3List.get(mCurrentMusicIndex));
                break;
            case R.id.iv_play:
                if (mMusicController.isPlaying()) {
                    mMusicController.pause();
                    mIvPlay.setImageDrawable(getResources().getDrawable(R.drawable.playbar_play));
                } else {
                    mMusicController.play();
                    mIvPlay.setImageDrawable(getResources().getDrawable(R.drawable.playbar_pause));
                }
                break;
            case R.id.iv_next:
                if (mCurrentMusicIndex == mMp3List.size() - 1) {
                    Toast.makeText(this, "当前已经是最后一首了", Toast.LENGTH_SHORT).show();
                    return;
                }
                mCurrentMusicIndex++;
                mMusicController.init(mMp3List.get(mCurrentMusicIndex));
                break;
        }


    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mMusicController.pause();
        // 解绑服务
        unbindService(mConnection);
        Intent intent = new Intent(this, MusicService.class);
        stopService(intent);
    }
}
